#!/usr/bin/env bash
srcDir=".."
pythonExec="${HOME}/anaconda3/bin/python3"

function usage() {
    echo "Usage: $( basename $0 ) [ -g | -n | -h ]"
    echo "    -g: 如果设置则建造gpu版本,否则建造cpu版本"
    echo "    -n: dry-run. 不实际运行，只显示要执行的命令"
    echo "    -h: 打印此信息"
    exit 1
}

function do_build() {

    echo "/usr/bin/cmake ${srcDir}/cmake \\"
    echo "    -Donnxruntime_DEV_MODE=ON \\"
    echo "    -DPython_EXECUTABLE=${pythonExec} \\"
    echo "    -DPYTHON_EXECUTABLE=${pythonExec} \\"
    echo "    -Donnxruntime_ENABLE_PYTHON=ON \\"
    echo "    -Donnxruntime_BUILD_SHARED_LIB=ON \\"
    echo "    -Donnxruntime_ENABLE_LANGUAGE_INTEROP_OPS=ON \\"
    echo "    -Donnxruntime_USE_NCCL=ON \\"
    echo "    -Donnxruntime_USE_MPI=ON \\"
    echo "    -Dgtest_force_shared_crt=ON \\"
    echo "    ${extraOptions}"

    if ${dryRun}
    then
        return
    fi
    
    sleep 2
    
    /usr/bin/cmake ${srcDir}/cmake \
        -Donnxruntime_DEV_MODE=ON \
        -DPython_EXECUTABLE=${pythonExec} \
        -DPYTHON_EXECUTABLE=${pythonExec} \
        -Donnxruntime_ENABLE_PYTHON=ON \
        -Donnxruntime_BUILD_SHARED_LIB=ON \
        -Donnxruntime_ENABLE_LANGUAGE_INTEROP_OPS=ON \
        -Donnxruntime_USE_NCCL=ON \
        -Donnxruntime_USE_MPI=ON \
        -Dgtest_force_shared_crt=ON \
        ${extraOptions}
}

dryRun=false
cudaDir="/usr/local/cuda-12.1"
# 默认build cpu version
extraOptions="-DCMAKE_BUILD_TYPE=Release"
while getopts "gnh" args
do
    case "${args}" in
        g)
            # 增加选项 
            extraOptions="${extraOptions} -Donnxruntime_USE_CUDA=ON"
            extraOptions="${extraOptions} -Donnxruntime_CUDA_HOME=/usr/local/cuda-12.1"
            extraOptions="${extraOptions} -Donnxruntime_CUDNN_HOME=/usr/local/cuda-12.1"
            ;;
        n)
            dryRun=true
            ;;
        *)
            usage;;
    esac
done

do_build
# make -j

# build wheel
# python ${srcDir}/setup.py bdist_wheel --wheel_name_suffix=gpu --cuda_version=12.1
